home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / INFO / PCCDEMO.ZIP / COMP1.EXE / SBPROG.PRS < prev    next >
Text File  |  1993-12-20  |  8KB  |  210 lines

  1.                                      ÆÄöìâ üïÇÆôäæ ÅæÄåæÇîîêìå ╬╠╬╠╬╠╧╧╬╠╬╠╡
  2.                                    
  3.                                    │     Sound   cards   are   becoming
  4.                                    │  increasingly more  common in PC's
  5.                                    │  and whilst  they make  games more
  6.                                    │  enjoyable,    and     PC    music
  7.               │ │  │               │  acceptable -  it's even  more fun
  8. ┌─┐┌─┐┌ ┌┌─┐┌─┤ ├─┐│┌─┐┌─┐├  ┌─┐┌─┐│  to use them  in programs your own
  9. └─┐│ ││ ││ ││ │ │ ││┌─┤└─┐│  ├─┘│  │  programs.   ≤τΣ    ∩±εµ±α∞   ≤τα≤
  10. └─┘└─┘└─┘└ └└─┘ └─┘┴└─┘└─┘└─┘└─┘┴  │  σεδδε÷≥   ≥τε÷≥   τε÷   ≤ε   ∩δα√
  11.                             .      │  Γ±Σα≤Φ⌡Σ δαß≥ ⌡εΓ σΦδΣ≥, α Γε∞∞εφ
  12. ┌─┐┌─┌─┐┌─┐┌─┐┌─┐┌─┬─┐┌─┬─┐┬┌─┐┌─┐ │  σε±∞α≤   σε±   ≥≤ε±Φφµ  πΦµΦ≤ΦⁿΣπ
  13. │ ││ │ ││ ││  ┌─┤│ │ ││ │ │││ ││ │ │  ≥ε⌠φπ≥.
  14. ├─┘┴ └─┘└─┤┴  └─┘└ └ └└ └ └┴└ └└─┤ │
  15. │         │                      │ │     This  program   is  writen  in
  16.         └─┘                    └─┘ │  Turbo Pascal,  however,  once the
  17.                                    │  basics   of   the   program   are
  18.                                    │  understood, it  can  be re-writen
  19.                                    │  in other languages.
  20.          ß√ ≥≤Σ⌡Σφ α±φεδπ          │     To understand  the program, an
  21.                                    │  intermediate   level   of   Turbo
  22.         Source Code: SB.PAS        │  Pascal experience  is  required -
  23.                                 
  24.  
  25. although    the     program    is  │  The program contains two elements
  26. relatively simple due  to its use  │  of Turbo  Pascal that  may be new
  27. of  the  CT-VOICE  driver.   This  │  to  you,  pointers  and  built-in
  28. driver,   provided   with   Sound  │  assembly.    Following    is   an
  29. Blaster  type  cards,  provides a  │  explanation of these.
  30. simple  programming  interface to  │
  31. the card,  bypassing the  need to  │
  32. control the hardware directly.     │
  33.                                    │  ÅÄêìôäæÆ
  34.    Using the driver also provides  │
  35. greater compatibility.   With new  │
  36. sound cards only the driver needs  │     A  pointer   is   literally  a
  37. to be  substituted  in  order for  │  variable that  points  to another
  38. your program to work.              │  variable.  So  instead of storing
  39.                                    │  a number or character string etc,
  40.    A file  called SB.PAS contains  │  it stores the  memory location of
  41. the full programs  listing, it is  │  another variable.   Following are
  42. available with  this  copy  of PC  │  examples  of  how  to  specify  a
  43. COMPLETE.                          │  pointer variable.
  44.                                    │
  45.  
  46.  
  47. var
  48.   test : pointer;
  49.   test1 : ^string;
  50.  
  51. In the  above  example, test can point to any variable, while test1 can only
  52. point to a string variable - this is checked by the compiler.  The following
  53. program fragment shows an example of their use.
  54.  
  55. var
  56.   a : word;
  57.   p1, p2 : ^word;
  58.  
  59. begin
  60.   a := 1;
  61.   p1 := @a;           {The "@" symbol means "the memory location of"
  62.                        so, p1 := the memory location of a}
  63.  
  64.   writeln('A = ', a, '  P1^ = ', p1^);
  65.  
  66.                       {The "^" symbol means the data stored at the memory
  67.                        
  68.                        location held by the preceding pointer variable.
  69.                        So in this case, the data stored at the address
  70.                        pointed to by p1}
  71.   p2 := p1;           {Pointers can be assigned in the same way as other
  72.                        variables.  So p2 := p1 means store in p2 the memory
  73.                        location stored in p1}
  74.  
  75.   writeln('A = ', a, '  P1^ = ', p1^, '  P2^ = ', p2^);
  76. end.
  77.  
  78.  
  79.                        ÅÄêìôäæÆ - âÿìÇîêé òÇæêÇüïäÆ
  80.  
  81.  
  82.  
  83.    A dynamic variable is one that can be created, and removed as desired,
  84. allowing the memory to be used for other purposes.   In Turbo Pascal they
  85. also have the advantage of being stored on the heap, as against the stack.
  86.    The stack is only 64k  in size,  where as the heap can  expand to 640k.
  87. Placing  large  variables  on  the  heap saves room for small loop control
  88. variables etc on the stack.  Following are some examples of their use.
  89.  
  90.  
  91. Note - a dynamic variable is always access via a pointer variable.
  92.  
  93. var
  94.   p1, p2 : ^string;
  95.   p3 : pointer;
  96. begin
  97.   new(p1);           {New allocates the memory required}
  98.   p1^ := 'Hello';    {Stores 'Hello' in space allocated}
  99.   dispose(p1);       {Free's up the allocated memory}
  100.  
  101.   getmem(p3, 256);   {Allocates 256 bytes, and points p3 to this memory}
  102.   p2 := p3;          {p2 now points to the same place as p3}
  103.   p2^ := 'Hello';    {The allocated memory now contains 'Hello'}
  104.   freemem(p3, 256);  {De-allocates the 256 bytes}
  105. end.
  106.  
  107.  
  108.                             ôçä üöêïô-êì ÇÆÆäîüïäæ
  109.  
  110. In Turbo Pascal 6.0 the built-in assembler was added.  
  111.  
  112.  
  113. This allows assembly language to be included simply in your Turbo Pascal 
  114. source code. Here is an example.
  115.  
  116. begin
  117.   writeln('Wow - assembly language');
  118.  
  119.   asm                     {'asm' specifies the start of any assembly code}
  120.     mov ah, 2             {This assembly code moves the cursor to (10,10)}
  121.     mov bh, 0
  122.     mov dh, 10-1
  123.     mov dl, 10-1
  124.     int 10h
  125.   end;                    {This 'end' specifies the end of the assembly}
  126.  
  127.   writeln('** Bye **');
  128. end.
  129.  
  130. If a  procedure or  function  needs only to be assembly only, it can be 
  131. declared as an assembly procedure/function, and improves the efficiency 
  132. of the code generated.  Here is an example.
  133.  
  134. procedure MoveTo(x, y : byte); assembler; {The 'assembler' declares this
  135.                                            procedure to be an assembly
  136.                                            procedure}
  137.  
  138. asm                                       {Notice no 'begin' is used}
  139.   mov ah, 2
  140.   mov bh, 0
  141.   mov dh, y                               {Variables can be included easily}
  142.   dec dh
  143.   mov dl, x
  144.   dec dl
  145.   int 10h
  146. end;
  147.  
  148. In the program "SB.PAS"  you  will notice that assembly statements are used
  149. extensively, as are pointer and dynamic variables.  Hopefully you this will
  150. help you understand their use, and their benefits.
  151.  
  152.  
  153.                          åäìäæÇï ÄÅäæÇôêÄì Äà ÅæÄåæÇî
  154.                          
  155.  
  156.    The CT-VOICE driver is  first  loaded into memory.  It basically provides
  157. a number of "procedures"  and  "functions"  that we require in order to play
  158. VOC files.  Once loaded the pointer variable "CTVDriver" points to the start
  159. of  the  driver  in  memory.  Calling  this  address  will "run" the driver,
  160. however it needs to know what it  is we want it to do.  Communication to the
  161. driver is via the  CPU's  registers.  
  162. Here is one of the procedures from the program -
  163.  
  164. >  procedure SetBaseAddr(address : word); assembler;
  165. >
  166. >  { Sets the base address of the sound card }
  167. >
  168. >  asm
  169. >    mov bx, 1
  170. >    mov ax, address
  171. >    call CTVDriver
  172. >  end;
  173.  
  174. First we put 1 into the CPU register "bx".  When the driver is called
  175. ie. on the line "call CTVDriver", it looks at the contents of "bx" and
  176. knows that you wish to set the base address of the sound card, and hence
  177.  
  178.  
  179. goes to it's "procedure" that does this.  The driver then looks at the
  180. "ax" register which contains the address of the sound card.  When the
  181. driver needs to return values, the registers are again used.
  182.  
  183. >  function InitSB:boolean; assembler;
  184. >
  185. >  { Initialises the sound card }
  186. >
  187. >  asm
  188. >    mov bx, 3
  189. >    call CTVDriver
  190. >    cmp ax, 0
  191. >    jne @false
  192. >    mov ax, word(TRUE)
  193. >    jmp @done
  194. >  @false:
  195. >    mov ax, word(FALSE)
  196. >  @done:
  197. >  end;            { Return is via AX }
  198.  
  199.  
  200. In "InitSB" the driver returns the result of initialisation in the "ax"
  201. register.  Our Turbo Pascal procedure then returns this result to its caller
  202. again through the "ax" register ñ  
  203.  
  204.  
  205.  
  206.  
  207.  
  208. Turbo Pascal is a trade mark of Borland International.
  209. Sound Blaster is a trade mark of Creative Labs, Inc.
  210.